﻿namespace _4_NovaAdmin.Web.Core.Idempotency;

public class RedisIdempotencyStore : IIdempotencyStore, ISingletonDependency
{
    private readonly StackExchange.Redis.IDatabase _redisDb;
    private readonly TimeSpan _expiration;

    public RedisIdempotencyStore(RedisClient client, IOptions<IdempotencyOptions> options)
    {
        _redisDb = client.GetDatabase();
        _expiration = TimeSpan.FromHours(options.Value.ExpirationHours);
    }

    public async Task<bool> TryCreateRecordAsync(string key, string response)
        => await _redisDb.StringSetAsync(key, response, _expiration, When.NotExists);

    public async Task<string?> GetResponseAsync(string key)
        => await _redisDb.StringGetAsync(key);
}
